﻿using System;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace Entidades.Configuracion
{
    public class XmlOperations
    {
        /// <summary>
        /// Method that create a XML File that represent the object
        /// received as argument.
        /// </summary>
        /// <param name="serializable"></param>
        /// <param name="fileName"></param>
        public void serialize( Object serializable, string fileName )
        {
            XmlSerializer serializer = new XmlSerializer( serializable.GetType() );

            StreamWriter writer = new StreamWriter( fileName );

            serializer.Serialize(writer, serializable);
            writer.Close();
        }

        /// <summary>
        /// This Method return an object representation from the XML File.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="pathXSD"></param>
        /// <returns></returns>
        public ConfiguracionXML deserialize(String path)
        {
            XmlSerializer serializer = new XmlSerializer( typeof( ConfiguracionXML ) );
            XmlReader reader = XmlReader.Create(path);

            ConfiguracionXML config = (ConfiguracionXML) serializer.Deserialize(reader);
            reader.Close();

            return config;
        }

        /// <summary>
        /// This Method return an object representation from the File obtained
        /// through the Stream object..
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public ConfiguracionXML deserialize( Stream stream )
        {
            XmlSerializer serializer = new XmlSerializer( typeof( ConfiguracionXML ) );
            XmlReader reader = XmlReader.Create(stream);

            ConfiguracionXML config = (ConfiguracionXML) serializer.Deserialize(reader);
            reader.Close();

            return config;
        }

    }
}